package com.heart.sell.service.impl;

import com.heart.sell.dao.ProductInfoMapper;
import com.heart.sell.dto.CartDTO;
import com.heart.sell.enums.ProductStatusEnum;
import com.heart.sell.enums.ResponseCodeEnum;
import com.heart.sell.exception.SellException;
import com.heart.sell.pojo.ProductInfo;
import com.heart.sell.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Luo Zhen
 * @create 2019-04-23 9:37
 */
@Service
public class DefaultProductServiceImpl implements ProductService {

    @Autowired
    private ProductInfoMapper productInfoMapper;


    @Override
    public ProductInfo findOne(String productId) {
        return productInfoMapper.findOne(productId);
    }

    @Override
    public List<ProductInfo> findUpAll() {
        return productInfoMapper.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return productInfoMapper.findAll(pageable);
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return productInfoMapper.save(productInfo);
    }

    @Override
    @Transactional(rollbackFor = SellException.class)
    public void increaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            ProductInfo product = productInfoMapper.findOne(cartDTO.getProductId());
            if (product == null) {
                throw new SellException(ResponseCodeEnum.PRODUCT_NOT_EXIST);
            }

            Integer result = product.getProductStock() + cartDTO.getProductQuantity();
            product.setProductStock(result);

            productInfoMapper.save(product);
        }
    }

    @Override
    @Transactional(rollbackFor = SellException.class)
    public void decreaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            ProductInfo product = productInfoMapper.findOne(cartDTO.getProductId());
            if (product == null) {
                throw new SellException(ResponseCodeEnum.PRODUCT_NOT_EXIST);
            }

            Integer result = product.getProductStock() - cartDTO.getProductQuantity();
            if (result < 0) {
                throw new SellException(ResponseCodeEnum.PRODUCT_STOCK_ERROR);
            }
            product.setProductStock(result);

            productInfoMapper.save(product);
        }
    }

    @Override
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo = productInfoMapper.findOne(productId);
        if (productInfo == null) {
            throw new SellException(ResponseCodeEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatusEnum() == ProductStatusEnum.UP) {
            throw new SellException(ResponseCodeEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        return productInfoMapper.save(productInfo);
    }

    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo = productInfoMapper.findOne(productId);
        if (productInfo == null) {
            throw new SellException(ResponseCodeEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatusEnum() == ProductStatusEnum.DOWN) {
            throw new SellException(ResponseCodeEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        return productInfoMapper.save(productInfo);

    }
}
